package com.sikaryofficial.tools;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sikaryofficial.domain.UserVO;
import jakarta.annotation.PostConstruct;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : wuweihong
 * @desc : TODO  请填写你的功能描述
 * @date : 2025-11-06
 */

@Service
public class UserInfoService {


	private Map<String, UserVO> userMap;

	@Value("classpath:manager.json")
	private Resource resource;

	@Autowired
	private ObjectMapper objectMapper;

	@PostConstruct
	public void init() {
		try {
			InputStream inputStream = resource.getInputStream();
			List<UserVO> userList = objectMapper.readValue(inputStream, objectMapper.getTypeFactory().constructCollectionType(List.class, UserVO.class));
			userMap = userList.stream().collect(java.util.stream.Collectors.toMap(UserVO::getNickName, user -> user));
		} catch (Exception e) {
			throw new RuntimeException("Failed to load user data", e);
		}
		System.out.println("WeatherService 工具已初始化");

	}

	@Tool(description = "查询指定城市的当前天气情况")
	public String lookupWeather(String city) {
		// 这里可替换为真实的 REST API/数据库查询逻辑。
		// 为了示例简单，返回一个固定格式。
		if (city == null || city.isBlank()) {
			return "未提供城市名称";
		}
		return "城市 " + city + "：晴，25°C，有大风";
	}

	@Tool(description = "查询指定名字的用户个人信息")
	public String getUserInfo(String name) {
		// 这里可替换为真实的 REST API/数据库查询逻辑。
		// 为了示例简单，返回一个固定格式。
		if (name == null || name.isBlank() || !userMap.containsKey(name)) {
			return "未提供用户姓名或用户不存在";
		}
		StringBuilder stringBuilder = new StringBuilder();
		UserVO userVO = userMap.get(name);
		stringBuilder.append("用户 ").append(name).append(" 的个人信息如下：\n");
		if (StringUtils.hasText(userVO.getCode())) {
			stringBuilder.append("工号：").append(userVO.getCode()).append("\n");
		}
		if (StringUtils.hasText(userVO.getPhone())) {
			stringBuilder.append("电话：").append(userVO.getPhone()).append("\n");
		}
		if (StringUtils.hasText(userVO.getDepartmentName())) {
			stringBuilder.append("部门：").append(userVO.getDepartmentName()).append("\n");
		}
		if (StringUtils.hasText(userVO.getByyx())) {
			stringBuilder.append("毕业院校：").append(userVO.getByyx()).append("\n");
		}
		if (StringUtils.hasText(userVO.getSyzy())) {
			stringBuilder.append("所学专业：").append(userVO.getSyzy()).append("\n");
		}
		if (StringUtils.hasText(userVO.getJobName())) {
			stringBuilder.append("职位：").append(userVO.getJobName()).append("\n");
		}
		return stringBuilder.toString();
	}
}
